package com.aw.baselib.widgets.RoundImageView

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.aw.baselib.util.AwDisplayUtil

class AwCircleView : View {
    private var mContext: Context
    private var paint: Paint? = null // 画笔对象
    private var colorStr = "#64E155"

    constructor(context: Context) : super(context) {
        mContext = context
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        mContext = context
        init()
    }

    private fun init() {
        paint = Paint()
        paint!!.isAntiAlias = true // 开启抗锯齿功能
    }

    fun setColor(colorStr: String) {
        this.colorStr = colorStr
        invalidate()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setMeasuredDimension(
            AwDisplayUtil.dip2px(mContext, 44f),
            AwDisplayUtil.dip2px(mContext, 44f)
        ) // 设置视图大小为正方形
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val centerX = width / 2 // 获取视图中心点 x 坐标
        val centerY = height / 2 // 获取视图中心点 y 坐标
        val radius = (Math.min(centerX, centerY) - 10).toFloat() // 计算半径值
        val rectF =
            RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius) // 构造矩形区域
        paint!!.color = Color.parseColor(colorStr)
        //        // 根据需要设置不同颜色的圆形背景
//        if (isSelected()) {
//            paint.setColor(Ciki==); // 选中状态时的颜色
//        } else {
//            paint.setColor(getResources().getColor(R.color.normal_circle)); // 非选中状态时的颜色
//        }
        canvas.drawArc(rectF, 0f, 360f, true, paint!!) // 绘制弧线
    }
}